package vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;

import controleur.ControleurPanelClasse;

import modele.ModeleClasse;
import modele.ModeleMethode;


public class VuePanelClasse extends JPanel  {
	
	private ModeleClasse m;
	private ControleurPanelClasse cpc;
	
	private static final long serialVersionUID = 1L;
	
	private JPanel panNom;
	private JPanel panAttributs; 
	private JPanel panMethodes;
	private JLabel labelNom;
	private JLabel labelAttributs;
	private JLabel labelMethodes;
	private int nbAtt;
	private int nbMeth;
	private int largeurMax;
	
	public int xPrec;
	public int yPrec;
	

	public VuePanelClasse(ModeleClasse mm) {
		this.m = mm;
		init();
	}
	
	public void colorier(Color c) {
		this.panNom.setBackground(c);
		this.panAttributs.setBackground(c);
		this.panMethodes.setBackground(c);
	}
	
	public void setControleur(ControleurPanelClasse c) {
		this.cpc = c;
	}
	
	public ControleurPanelClasse getControleur() {
		return this.cpc;
	}
	
	public ModeleClasse getModele() {
		return this.m;
	}
	
	public void setModele(ModeleClasse mc) {
		this.m = mc;
		init();
	}
	
	public void init() {
		this.panNom = new JPanel();
		this.panAttributs = new JPanel();
		this.panMethodes = new JPanel();
		
		this.labelNom = new JLabel();
		this.labelAttributs = new JLabel();
		this.labelMethodes = new JLabel();

		this.panNom.add(labelNom);
		this.panAttributs.add(labelAttributs);
		this.panMethodes.add(labelMethodes);
		
		labelNom.setText(this.m.getNom());
		
		char[] c = this.m.getNom().toCharArray();
		largeurMax = c.length;
		
		/* Affichage des attributs */
		nbAtt = this.m.getAttributs().size();
		String attributs = "<html>";
		for(int i = 0 ; i < nbAtt ; i ++) {
			attributs += "+ "+this.m.getAttributs().get(i).getNom();
			attributs += " : " + this.m.getAttributs().get(i).getType() +"<br/>";
			
			c = this.m.getAttributs().get(i).getNom().toCharArray();
			if(c.length > largeurMax)
				largeurMax = c.length;
		}
		labelAttributs.setText(attributs+"</html>");
		
		/* Affichage des méthodes */
		boolean methodesAvecParametres = false;
		nbMeth = this.m.getMethodes().size();
		String methodes = "<html>";
		String methodesTipText = "<html>";
		for(int i = 0 ; i < nbMeth ; i ++) {
			ModeleMethode modelMeth = this.m.getMethodes().get(i);
			
			methodes += "+ "+ modelMeth.getNom()+"(";
			methodesTipText += "+ "+ modelMeth.getNom()+"(";
			
			for(String v : modelMeth.getListeArgs()) {
				if(!v.equals(""))
					methodesTipText += v + ", ";
			}
			
			if(modelMeth.getListeArgs().size() > 0 && !modelMeth.getListeArgs().get(0).equals("")) {
				methodesAvecParametres = true;
				methodesTipText = methodesTipText.substring(0, methodesTipText.length()-2);
				methodes += "...";
			}
			
			methodes += ") : ";
			methodesTipText += ") : ";
			methodes += modelMeth.getTypeRetour();
			methodesTipText += modelMeth.getTypeRetour();
			methodes += "<br/>";
			methodesTipText += "<br/>";

			c = this.m.getMethodes().get(i).getNom().toCharArray();
			if(c.length > largeurMax)
				largeurMax = c.length;
		}
		labelMethodes.setText(methodes+"</html>");
		if(methodesAvecParametres) {
			labelMethodes.setToolTipText(methodesTipText);
		}
		
		int largeur = 10 + 20 * largeurMax;
		
		this.panNom.setPreferredSize(new Dimension(largeur,25));
		this.panNom.setBorder(BorderFactory.createLineBorder(Color.black));
		this.panNom.setBackground(new Color(255,204,102));
		
		this.panAttributs.setPreferredSize(new Dimension(largeur,10+(20*nbAtt)));
		this.panAttributs.setBorder(BorderFactory.createLineBorder(Color.black));
		this.panAttributs.setBackground(new Color(255,204,102));
		
		this.panMethodes.setPreferredSize(new Dimension(largeur,10+(20*nbMeth)));
		this.panMethodes.setBorder(BorderFactory.createLineBorder(Color.black));
		this.panMethodes.setBackground(new Color(255,204,102));
		
		this.setSize(new Dimension(largeur,25+20+(nbAtt*20)+(nbMeth*20)));
		this.setBorder(BorderFactory.createLineBorder(Color.black));
		
		if(this.getComponentCount()>0) {
			this.removeAll();
		}
		
		this.setLayout(new BorderLayout());
		
		this.add(panNom, BorderLayout.NORTH);
		this.add(panAttributs, BorderLayout.CENTER);
		this.add(panMethodes, BorderLayout.SOUTH);
		
		this.repaint();
	}
}
